package gaury.ssh;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.SftpClient;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.session.SessionChannelClient;

public class SSHClient {
	
	private SshClient sshClient;
	private SftpClient sftpClient;
	private PasswordAuthenticationClient pwdAuthClient;
	private SessionChannelClient sessionChannelClient;
	
	private String userName;
	private String userPwd;
	private String host;
	private int port;
	
	private List<String> resultList;
	
	public SSHClient(String userName, String userPwd, String host, int port)
	{
		this.userName = userName;
		this.userPwd = userPwd;
		this.host = host;
		this.port = port;
		
		this.pwdAuthClient = new PasswordAuthenticationClient();
		this.pwdAuthClient.setUsername(userName);
		this.pwdAuthClient.setPassword(userPwd);
		
		this.sshClient = new SshClient();
		
		resultList = new ArrayList<String>();
	}
	
	public void connect()
	{	
		try {
			this.sshClient.setSocketTimeout(20000);
			this.sshClient.connect(this.host, this.port);
			int result = this.sshClient.authenticate(this.pwdAuthClient);
			
			if(result == AuthenticationProtocolState.COMPLETE)
			{
				System.out.println("Complete");
			}
			else
			{
				System.out.println("Fail");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void disConnect()
	{
		if(this.sshClient.isConnected())
		{
			this.sshClient.disconnect();
		}
	}
	
	public List<String> executeCommand(String command)
	{
		List<String> rsList = new ArrayList<String>(); 
		
		try {
			rsList.add("excute Command : "+command);
			this.resultList.add("excute Command : "+command);
			
			this.sessionChannelClient = sshClient.openSessionChannel();
			this.sessionChannelClient.executeCommand(command);
			
			rsList = executeResult();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rsList;
	}
	
	public List<String> executeResult()
	{
		String line = "";
		InputStream is = this.sessionChannelClient.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		
		List<String> rsList = new ArrayList<String>(); 
		
		try {
			while((line = br.readLine()) != null)
			{
				rsList.add(line);
				this.resultList.add(line);
			}
			
			is = this.sessionChannelClient.getStderrInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			
			while((line = br.readLine()) != null)
			{
				rsList.add(line);
				this.resultList.add(line);
			}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rsList;
	}
	
	public List<String> getResultList()
	{
		return resultList;
	}
	
	public void sftpOpen()
	{
		try {
			this.sftpClient = this.sshClient.openSftpClient();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public void sftpClose()
	{
		try {
			if(!this.sftpClient.isClosed())
			{
				this.sftpClient.quit();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void get(String fileName)
	{
		String localDir = this.sftpClient.lpwd();
		String localFilePath = localDir+"/"+fileName;
		System.out.println("localDir : "+localDir);
		List<String> rsList = this.executeCommand("pwd");
		String remoteDir = rsList.get(1);
		System.out.println("remoteDir : "+remoteDir);
		try {
			this.sftpClient.cd(remoteDir);
			this.sftpClient.get(fileName, localFilePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void put(String filePath)
	{
		List<String> rsList = this.executeCommand("pwd");
		String remoteDir = rsList.get(1);
		System.out.println("remoteDir : "+remoteDir);
		try {
			this.sftpClient.cd(remoteDir);
			this.sftpClient.put(filePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
